//
//  NetWorkService2.swift
//  WxToutiao
//
//  Created by YQ05 on 2018/4/8.
//  Copyright © 2018年 yons. All rights reserved.
//

import Foundation
import Moya

enum NetworkService2 {
      case category

//    case submitComment(postId: Int, name: String, email: String, content: String)
      //case showCatcodeList(id: Int)
    
      case getNewsCateList(id: Int)
      case getProCateList(id: Int)  //获取产品信息
}

extension NetworkService2: TargetType {
    
    var headers: [String : String]? {
        return ["Content-type": "application/json"]
    }
    
    
    
    var baseURL: URL {
        //let baseUrl = "https://www.jmwebseo.cn/api"
        let baseUrl = "http://nhahomenews.com/app/"

        return URL(string: baseUrl)!
    }
    
    var path: String {
        switch self {
        case .category:
            return "/get-categories/"
       
        case .getNewsCateList:
            return "/get-news/"
            
        case .getProCateList:
            return "/get-products/"
            
        
        }
       
    }
    
    var method: Moya.Method {
        switch self {

        case .category:
             return .get
//        case .showCatcodeList:
//            return .get
//        case .showproductCatList:
//            return .get
            
        case .getNewsCateList:
            return .get
        case .getProCateList:
            return .get
            
        }
    }
    
    var sampleData: Data {
        switch self {
        case .category:
            return "category test data".utf8Encoded
            
//        case .showCatcodeList(let id):
//            return "catcode id is \(id)".utf8Encoded
            
        case .getNewsCateList(let id):
            return "getNewsCateList is \(id)".utf8Encoded
            
        case .getProCateList(let id):
            return "getProCateList test data \(id)".utf8Encoded
            

//        case .showproductCatList:
//            return "showproductCatList test data".utf8Encoded
            
            //        case .category:
            //            return "category test data".utf8Encoded
            //        case .showCateNewsList(let id):
            //            return "newslist id is \(id)".utf8Encoded
            //        case .submitComment(let postId, let name, let email, let content):
            //            return "\(postId),\(name),\(email),\(content)".utf8Encoded
        }
    }

    
    var task: Task {
        switch self {
        case .category:
           return .requestPlain
            
        case .getNewsCateList(let id):
            return .requestParameters(parameters: ["type" : id], encoding: URLEncoding.queryString)
            
        case .getProCateList(let id):
            return .requestParameters(parameters: ["catcode" : id], encoding: URLEncoding.queryString)
            
//        case .showproductCatList:
//            return .requestPlain
//        case .showCateNewsList(let id):
//            return .requestParameters(parameters: ["id" : id], encoding: URLEncoding.queryString)
//        case .submitComment(let postId, let name, let email, let content):
//            return .requestParameters(parameters: ["post_id": postId, "name": name, "email": email, "content": content], encoding: URLEncoding.queryString)
            
//        case .showCatcodeList(let id):
//            return .requestParameters(parameters: ["catcode" : id], encoding: URLEncoding.queryString)
            

            
        }
    }
    
}

// MARK: - Helpers
private extension String {
    var urlEscaped: String {
        return self.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
    }
    
    var utf8Encoded: Data {
        return self.data(using: .utf8)!
    }
}

